package com.jstyle.beatecg.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import com.jstyle.beatecg.app.MyApplication;
import com.jstyle.beatecg.util.ScreenUtils;

public class EcgWaveView extends View {
    private EcgViewInterface g;
    private int h = 0;
    Bitmap a;
    Bitmap b;
    Bitmap c;
    Canvas d = null;
    Canvas e = null;
    Canvas f = null;
    private Paint i;//波形画笔
    private Paint cicle=new Paint();//圆形光标
    private Paint j = new Paint();
    private int k;
    private int l;
    private boolean m = true;
    private int n;
    private int o;
    public double n_frequency;
    public int bx;
    public int by;
    public double change_y;
    public int change_50n;
    public double change_nV;
    public int change_h;
    public double change_x;
    public float n_step = 0f;//0.5步一个点
    private float p;
    private int q;
    public int n_Btime;
    private double r;
    private float s;
    private double[] t = new double[3];
    private float u;
    private float v;
    private static int w = 100;
    private int x;
    private int y;
    private int z;
    private int A;
    private boolean B = true;

    public EcgViewInterface getListener() {
        return this.g;
    }

    public void setListener(EcgViewInterface var1) {
        this.g = var1;
    }

    public boolean isWaveAdapter() {
        return this.m;
    }

    public void setWaveAdapter(boolean var1) {
        this.m = var1;
    }

    public int getSampleRe() {
        return this.o;
    }

    public void setSampleRe(int var1) {
        this.o = var1;
    }

    public int getSampleV() {
        return this.n;
    }

    public void setSampleV(int var1) {
        this.n = var1;
    }

    public double getN_frequency() {
        return this.n_frequency;
    }

    public void setN_frequency(double var1) {
        this.n_frequency = var1;
    }

    public EcgWaveView(Context var1, int var2, int var3) {
        super(var1);
        this.k = var2;
        this.l = var3;
    }

    public void init() {
        this.bx = this.k;
        this.by = this.l / 50 * 50 ;
        this.a();
        this.a = Bitmap.createBitmap(this.c, 0, 0, this.k, this.l);
        this.b = Bitmap.createBitmap(this.c, 0, 0, this.k, this.l);
        this.d = new Canvas();
        this.d.setBitmap(this.a);
        this.i = new Paint(4);
        this.i.setColor(Color.parseColor("#272727"));
        this.i.setStyle(Paint.Style.STROKE);
        this.i.setStrokeWidth(ScreenUtils.dip2px(MyApplication.context,1.3F));//心电线宽
        this.i.setAntiAlias(true);
        this.i.setDither(true);
        this.i.setStrokeJoin(Paint.Join.ROUND);
        this.e = new Canvas();
        this.e.setBitmap(this.b);
        this.s = this.l/9900;
       // this.p = this.k;
        this.p = 0;
        this.q = 0;
        this.x = 0;
        this.y = this.by;
        this.z = 2;
        this.change_h = this.by;
        this.change_50n = 50;
        this.change_x = 2.0D;
        this.change_y = 0.5D;
       // this.n_step = 1f;
        this.n_step = (float)(this.l)/300f;
        this.UnitDraw();
    }

    public void UnitDraw() {
        this.change_nV = 0.25D;
        this.r = 1000.0D / this.n_frequency;
        this.n_Btime = (int)(this.r * (double)this.change_50n / (double)this.n_step);
      //  this.g.onShowMessage(this.n_Btime + "", 0);
    }

    private void a() {
        this.c = Bitmap.createBitmap(this.k, this.l, Bitmap.Config.ARGB_8888);
        this.f = new Canvas();
        this.f.setBitmap(this.c);
        Paint var3 = new Paint();
        var3.setColor(Color.parseColor("#eeeeee"));//背景颜色
        var3.setStyle(Paint.Style.FILL);
        var3.setStrokeWidth(ScreenUtils.dip2px(MyApplication.context,1F));//大网格线框
        var3.setAntiAlias(true);
        var3.setDither(true);
        var3.setStrokeJoin(Paint.Join.ROUND);
        this.f.drawRect(0.0F, 0.0F, (float)this.bx, (float)this.by, var3);
        var3.setStyle(Paint.Style.STROKE);
       // var3.setColor(Color.argb(128, 220, 190, 50));
        var3.setColor(Color.parseColor("#D73E3E"));

        int var1;
        int var2;

        DashPathEffect pathEffect = new DashPathEffect(new float[] { 2,1,2,1}, 1);
        Paint paint = new Paint();
        paint.reset();
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(ScreenUtils.dip2px(MyApplication.context,0.5F));
        int color=0x80D73E3E;
        paint.setColor(color);
        paint.setAntiAlias(true);
      //  paint.setPathEffect(pathEffect);


        for(var1 = 0; var1 < this.bx; var1 += (this.by/6)/5) {
            this.f.drawLine((float)var1, 0.0F, (float)var1, (float)(this.by), paint);
        }
        for(var2 = 0; var2 < this.by; var2 += (this.by/6)/5) {
            this.f.drawLine(0.0F, (float)var2, (float)this.bx, (float)var2, paint);
        }
        for(var1 = 0; var1 < this.bx; var1 += this.by/6) {
            this.f.drawLine((float)var1, 0.0F, (float)var1, (float)(this.by), var3);
        }
        for(var2 = 0; var2 < this.by; var2 += this.by/6) {
            this.f.drawLine(0.0F, (float)var2, (float)this.bx, (float)var2, var3);
        }

    }

    public void drawWave(final float var1) {
        float var2;

        var2 = this.a(var1);
        this.i.setAntiAlias(true);
        this.i.setDither(true);
        this.d.drawLine(this.p, this.s,(this.p + this.n_step), (float)var2, this.i);
        this.p += this.n_step;
        this.b();
        this.invalidate();
        this.s = var2;
    }

    private void b() {
        Rect var1;
       if (this.p > this.bx) {
            if ((this.x - this.y) * 2 < this.by) {
                this.B = true;
                this.z = 1;
            }

            this.p = 0;
            var1 = new Rect(0, 0, ScreenUtils.dip2px(MyApplication.context,20F), this.l);
            this.d.drawBitmap(this.b, var1, var1, this.j);
            if (this.B && this.m) {
              //  this.AutoResize();
            }
            this.checkRange();
        }


        var1 = new Rect((int)(this.p + this.n_step + ScreenUtils.dip2px(MyApplication.context,20F)), 0, (int)(this.p + this.n_step + ScreenUtils.dip2px(MyApplication.context,30F)), this.l);//透明范围
        this.d.drawBitmap(this.b, var1, var1, this.j);




    }



    public void checkRange() {
        this.x = -3 * this.by;
        this.y = 3 * this.by;
    }

    private float a(float var1) {
        float valuecaten=this.l/2;//屏幕中心
        float valuemian=((300f / (float)this.l)*10f);//屏幕比列
        float var=(float)(valuecaten-(var1/valuemian));
        return var;


      /*  float var2 = (float)this.n * var1 / (float)this.o;
        int var3 = (short)((int)((double)this.change_h - ((double)var2 - this.change_y) * (double)this.change_50n / this.change_nV));
        if (var3 < this.y) {
            this.y = var3;
        }

        if (var3 > this.x) {
            this.x = var3;
        }

        if (var3 > this.by) {
            var3 = (short)this.by - 1;
            this.B = true;
            this.z = 1;
        }

        if (var3 <= 0) {
            var3 = 1;
            this.B = true;
            this.z = 1;
        }

        return var3;*/
    }

    @SuppressLint("DrawAllocation")
    public void onDraw(Canvas var1) {
        //var1.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG));
        var1.drawBitmap(this.a, 0.0F, 0.0F, this.j);//绘制透明区域
        //画光标
        cicle.setColor(Color.parseColor("#D73E3E"));
        if(0f!=this.p&&this.s!=0f){
            var1.drawCircle(this.p,this.s,ScreenUtils.dip2px(MyApplication.context,3F),cicle);
        }

    }

/*    public boolean onTouchEvent(MotionEvent var1) {
        switch(var1.getAction() & 255) {
            case 0:
                Log.i("tag", "手势放下");
                this.h = 1;
                break;
            case 1:
                this.h = 0;
            case 2:
            case 3:
            case 4:
            default:
                break;
            case 5:
                Log.i("tag", "手势放下" + this.a(var1));
                this.u = this.a(var1);
                ++this.h;
                break;
            case 6:
                Log.i("tag", "手势拿起" + this.a(var1));
                this.v = this.a(var1);
                --this.h;
                if (this.v > this.u + (float)w) {
                    this.n_step *= 2;
                    this.n_Btime = (int)(this.r * (double)this.change_50n / (double)this.n_step);
                    this.g.onShowMessage(this.n_Btime + "", 0);
                    Log.i("tag", "n_step-->" + this.n_step + ";n_Btime-->" + this.n_Btime);
                } else if (this.v < this.u - (float)w && this.n_step >= 2) {
                    this.n_step /= 2;
                    this.n_Btime = (int)(this.r * (double)this.change_50n / (double)this.n_step);
                    this.g.onShowMessage(this.n_Btime + "", 0);
                    Log.i("tag", "n_step-->" + this.n_step + ";n_Btime-->" + this.n_Btime);
                }
        }

        return true;
    }*/

    private float a(MotionEvent var1) {
        float var2 = var1.getX(0) - var1.getX(1);
        float var3 = var1.getY(0) - var1.getY(1);
        return (float)Math.sqrt((double)(var2 * var2 + var3 * var3));
    }
}
